﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using FarseerPhysics.Dynamics.Contacts;
using RacePlatformer.Manager;
using FarseerPhysics;

namespace RacePlatformer
{
    public class GameObject
    {
        public float rotation;
        public Vector2 position;
        public float mass;
        public Vector2 size;
        public Body body;
        protected World world;
        protected Texture2D texture;
        protected MainGame maingame;
        public float scale;
        public float layer;
        public int levelID;
        private Animation animation;
        protected Animation mainAnimation;
        public SpriteEffects desiredSpriteEffect = SpriteEffects.None;
        public GameObject(MainGame maingame, World world, Vector2 size, Vector2 position, BodyType bodytype, float mass,int levelID,float layer = 0.5f,float scale = 1.0f, float rotation = 0.0f)
        {
            this.maingame = maingame;
            this.world = world;
            this.position = position;
            this.mass = mass;
            this.rotation = rotation;
            this.scale = scale;
            this.layer = layer;
            this.size = size;
            this.levelID = levelID;
            this.body = BodyFactory.CreateRectangle(world, UnitsConverter.ToSimUnits(size.X),UnitsConverter.ToSimUnits(size.Y), 1.0f, new Vector2(UnitsConverter.ToSimUnits(position.X + (size.X/2)),UnitsConverter.ToSimUnits(position.Y+(size.Y/2))));
            this.body.BodyType = bodytype;
            this.body.Mass = mass;
            this.body.OnCollision += OnCollision;
            Initialize();
            LoadContent();
        }
        /// <summary>
        /// Constructor for the player
        /// </summary>
        public GameObject(MainGame maingame, World world, Vector2 position,int levelID)
        {
            this.maingame = maingame;
            this.world = world;
            this.levelID = levelID;
            this.position = position;
            LoadContent();
            Initialize();
        }

        private bool OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
        {
            return true;
        }
        public void Initialize()
        {
        }
        public virtual void Draw(GameTime gametime, SpriteBatch batch)
        {
            if (mainAnimation != null)
            {
                batch.Draw(mainAnimation.texture, UnitsConverter.ToDisplayUnits(this.body.Position), new Rectangle((int)(mainAnimation.currentFrame * mainAnimation.frameWidth), 0, (int)mainAnimation.frameWidth, (int)mainAnimation.frameHeight), Color.White, rotation, new Vector2((mainAnimation.frameWidth / 2), (mainAnimation.frameHeight / 2)), scale, desiredSpriteEffect, layer);
            }
            else
            {
                batch.Draw(texture, UnitsConverter.ToDisplayUnits(this.body.Position), null, Color.White, this.body.Rotation, new Vector2(texture.Width / 2, texture.Height / 2), scale, SpriteEffects.None, layer);
            }
            }
        public virtual void Update(GameTime gametime)
        {

        }
        public virtual void LoadContent()
        {
        }
    }
}
